package com.changgou.goods.controller;
import com.changgou.entity.Page;
import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.service.BrandService;
import com.changgou.goods.pojo.Brand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@RestController
@CrossOrigin
@RequestMapping("/brand")
public class BrandController {


    @Autowired
    private BrandService brandService;


    @GetMapping("/cate/{id}")
    public Result findByCategoryId(@PathVariable("id") Integer id){
        List<Brand> byCategoryId = brandService.findByCategoryId(id);
        return new Result(true,StatusCode.OK,"查询成功",byCategoryId);
    }



    /**
     * 根据分类名称查询品牌
     * @param name
     * @return
     */
    @GetMapping("/category/{name}")
    public Result<List<Map>> findByCategoryName(@PathVariable String name){
        List<Map> byCategory = brandService.findByCategory(name);
        return new Result<>(true,StatusCode.OK,"查询成功",byCategory);

    }


    /**
     * 查询全部数据
     * @return
     */
    @GetMapping
    public Result<List<Brand>> findAll(){
        try {
            System.out.println("开始睡觉");
            Thread.sleep(3000);
            System.out.println("睡醒啦");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        List<Brand> brandList = brandService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",brandList) ;
    }

    /***
     * 根据ID查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable Integer id){
        Brand brand = brandService.findById(id);
        return new Result(true,StatusCode.OK,"查询成功",brand);
    }


    /***
     * 新增数据
     * @param brand
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true,StatusCode.OK,"添加成功");
    }


    /***
     * 修改数据
     * @param brand
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody Brand brand,@PathVariable Integer id){
        brand.setId(id);
        brandService.update(brand);
        return new Result(true,StatusCode.OK,"修改成功");
    }


    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        brandService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
        条件查询
     */
    @PostMapping(value = "/search" )
    public Result<List<Brand>> findList(@RequestBody(required = false) Brand brand){
        List<Brand> list = brandService.findList(brand);
        return new Result<List<Brand>>(true,StatusCode.OK,"查询成功",list);
    }

    /**
     * 分页查询
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable  int page, @PathVariable  int size){
        Page<Brand> page1 = brandService.findPage(page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",page1);
    }

    /**
     * 分页查询
     */
    @GetMapping(value = "/searchCondition/{page}/{size}" )
    public Result<Page> findPageByCondition(@RequestBody Brand brand, @PathVariable  int page, @PathVariable  int size){
        Page<Brand> page1 = brandService.findPage(brand,page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",page1);
    }



}
